package com.immortal.core.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicInteger;

public class SerialUtil {
    private final static AtomicInteger SEQ3= new AtomicInteger(Integer.MAX_VALUE);
    private final static char[] BASE_CHARS=new char[]{'0','1','2','3','4','5','6','7','8','9'} ;
    public static String nextseq3(){
        int nextSequence=SEQ3.incrementAndGet();
        if(nextSequence>=0){
            nextSequence=Integer.MAX_VALUE-nextSequence;
        }else{
            nextSequence=Integer.MAX_VALUE+1+nextSequence;
        }
        int mod=nextSequence%(BASE_CHARS.length*BASE_CHARS.length*BASE_CHARS.length);
        int mod1=nextSequence%(BASE_CHARS.length*BASE_CHARS.length);
        return BASE_CHARS[mod/(BASE_CHARS.length* BASE_CHARS.length)]+""+ BASE_CHARS[mod1/ BASE_CHARS.length]+""+BASE_CHARS[mod1% BASE_CHARS.length];
    }

    /**
     * 21位 流水号(19位时间+ 3位顺序码)
     * @return
     */
    public static String serialNo(){
       return  LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"))+nextseq3();
    }
}
